package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.TShelves;
import com.hopu.blb_erp.entity.TWarehouse;
import com.hopu.blb_erp.service.TShelvesService;
import com.hopu.blb_erp.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Api
@RestController
public class TShelvesController {

    @Autowired
    //
    private TShelvesService tShelvesService;

    @ApiOperation("分页查询货架")
    @GetMapping("/shelves/{current}/{pageSize}")
    public ResponseEntity<PageEntity> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize,
                                               String shelvesNo,String date){
        PageEntity<TShelves> page = tShelvesService.findPage(current, pageSize,shelvesNo,date);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("通过库区编号查询所有货架")
    @GetMapping("/shelve/{warehouseId}")
    public ResponseEntity<List<TShelves>> findByWarehouseId(@PathVariable String warehouseId){
        List<TShelves> byWarehouseId = tShelvesService.findByWarehouseId(warehouseId);
        return ResponseEntity.ok(byWarehouseId);
    }

    @ApiOperation("添加货架")
    @PostMapping("/shelve")
    public ResponseEntity<String> add(@RequestBody TShelves tShelves){
        LocalDateTime now = LocalDateTime.now();
        //设为当前中国时间
        Timestamp timestamp = Timestamp.valueOf(now);
        tShelves.setCreatTime(timestamp);
        tShelvesService.save(tShelves);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("删除货架")
    @DeleteMapping("/shelve/{id}")
    public ResponseEntity<String> delete(@PathVariable("id") Long id){
        tShelvesService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("更新货架")
    @PutMapping("/shelve")
    public ResponseEntity<String> modify(@RequestBody TShelves tShelves){
        tShelvesService.updateById(tShelves);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("修改库区状态")
    @PutMapping("/shelve/{id}/{status}")
    public ResponseEntity<String> update(@PathVariable("id") Integer id,@PathVariable("status")boolean status){
        TShelves tShelves = new TShelves();
        tShelves.setId(id);
        tShelves.setStatus(status);
        tShelvesService.updateById(tShelves);
        return ResponseEntity.ok("ok");
    }
}
